package com.bike.bikeserver.common.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.io.File;
import java.util.Collections;

/**
 * @Author: ZZX
 * @description: 代码生成器
 * @date: 2023-07-14 13:03
 */
public class CodeGenerator
{
    public static void main(String[] args)
    {
        generator();
        // Move the generated files to the desired directories
        moveFiles();
    }

    private static void generator()
    {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/bikedb", "root", "underdog").globalConfig(builder ->
                {
                    builder.author("ZZX") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
//                            .fileOverride() // 覆盖已生成文件
                            .outputDir("F:\\items\\bike-server\\src\\main\\java"); // 指定输出目录
                }).packageConfig(builder ->
                {
                    builder.parent("com.bike.bikeserver") // 设置父包名
                            .moduleName(null) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "F:\\items\\bike-server\\src\\main\\java\\com\\bike\\bikeserver\\mapper\\xml\\")); // 设置mapperXml生成路径
                }).strategyConfig(builder ->
                {
                    builder.entityBuilder().enableLombok();
                    builder.mapperBuilder().enableMapperAnnotation().build();
                    builder.controllerBuilder().enableHyphenStyle() // 开启驼峰转连字符
                            .enableRestStyle(); // 开启生成@RestController 控制器
                    builder.addInclude("model_test_log") // 设置需要生成的表名
                            .addTablePrefix("sys_"); // 设置过滤表前缀
                })
                // .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    private static void moveFiles()
    {
        // Move the entity classes to the 'domain' folder
        moveFile("F:\\items\\bike-server\\src\\main\\java\\com\\bike\\bikeserver\\entity", "F:\\items\\bike-server\\src\\main\\java\\com\\bike\\bikeserver\\domain");

        // Move the mapper interfaces to the 'mapper' folder
        moveFile("F:\\items\\bike-server\\src\\main\\java\\com\\bike\\bikeserver\\mapper", "F:\\items\\bike-server\\src\\main\\java\\com\\bike\\bikeserver\\mapper");

        // The xml files are already generated in the correct folder
    }

    private static void moveFile(String sourcePath, String targetPath)
    {
        File sourceDir = new File(sourcePath);
        File targetDir = new File(targetPath);

        if (!sourceDir.exists())
        {
            System.out.println("Source directory does not exist: " + sourcePath);
            return;
        }

        if (!targetDir.exists())
        {
            targetDir.mkdirs();
        }

        File[] files = sourceDir.listFiles();
        if (files != null)
        {
            for (File file : files)
            {
                if (file.isFile())
                {
                    file.renameTo(new File(targetDir, file.getName()));
                }
            }
        }

        // Delete the source directory after moving the files
        sourceDir.delete();
    }
}
